package com.fqh.controller;

import com.fqh.config.URLconfig;
import com.fqh.entity.Fooddetail;
import com.fqh.entity.JsonResult;
import com.fqh.service.FoodClassService;
import com.fqh.service.FoodDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class FoodDetailController {

    @Autowired
    private FoodDetailService detailService;
    @Autowired
    private FoodClassService classService;

    @RequestMapping("/findAllDetail.do")
    public Map findAllDetail(Integer page, Integer limit, String search) {
        List<Fooddetail> fooddetail = detailService.findAllDetail(page, limit, search);
        Integer count = detailService.findCount(search);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", fooddetail);
        map.put("count", count);
        return map;
    }

    @RequestMapping("/upload.do")
    public String uploadFile(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile commonsMultipartFile) throws IOException {
        request.setCharacterEncoding("utf-8");
        String filename = commonsMultipartFile.getOriginalFilename();
        int index = filename.lastIndexOf(".");
        String extName = filename.substring(index);
        UUID uuid = UUID.randomUUID();
        String name = uuid + extName;
        //String filePath = request.getScheme() + "://" + request.getServerName() + ":" +request.getServerPort() + "/Boheyuan_Manage_war_exploded/img/food/" + name;
        File file = new File(URLconfig.FileName, name);
        commonsMultipartFile.transferTo(file);
        Fooddetail detail = new Fooddetail();
        String foodName = request.getParameter("foodName");
        String foodclass = request.getParameter("foodclass");
        Integer classId = classService.findFoodIdByClassName(foodclass);
        String fooddetail = request.getParameter("desc");
        String rate = request.getParameter("rate");
        String price = request.getParameter("price");
        if (price == null || price.length() == 0) {
            price = "0";
        }
        detail.setFoodClassid(classId);
        detail.setFoodDetail(fooddetail);
        detail.setFoodEvaluate(Integer.parseInt(rate));
        detail.setFoodImage(name);
        detail.setFoodPrice(Double.parseDouble(price));
        detail.setFoodName(foodName);
        try {
            Integer count = detailService.addFoodDetail(detail);
            return "<script>window.parent.location.reload();</script>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<script>alert('添加失败')</script>";

        }
    }

    @RequestMapping("/deleteByFoodClassid.do")
    public JsonResult deleteByFoodClassid(Integer id, String img) {
        JsonResult result = null;
        try {
            detailService.deleteByFoodClassid(id);
            File file = new File(URLconfig.FileName, img);
            file.delete();
            result = new JsonResult("删除成功", true);
        } catch (Exception e) {
            result = new JsonResult("删除失败", false);
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/findFooddetailByFoodId.do")
    public Fooddetail findFooddetailByFoodId(@RequestParam("foodId") Integer id) {
        Fooddetail fooddetai = detailService.findFooddetailByFoodId(id);
        return fooddetai;
    }

    @RequestMapping("/updateFoodDetail.do")
    public JsonResult updateFoodDetail(Integer foodId, String foodName, String foodclass, String desc, Integer rate, Double price) {
        Integer classId = classService.findFoodIdByClassName(foodclass);
        Fooddetail fooddetail = new Fooddetail();
        fooddetail.setFoodId(foodId);
        fooddetail.setFoodName(foodName);
        fooddetail.setFoodClassid(classId);
        fooddetail.setFoodDetail(desc);
        fooddetail.setFoodPrice(price);
        fooddetail.setFoodEvaluate(rate);
        JsonResult result = null;
        try {
            detailService.updateFoodDetail(fooddetail);
            result = new JsonResult("修改成功", true);
        } catch (Exception e) {
            result = new JsonResult("修改失败", false);
            e.printStackTrace();
        }
        return result;
    }
}
